﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RuoVea.ExDto;
using RuoVea.ExEnum;
using RuoVea.ExSugar.Entity;
using RuoVea.OmiApi.Article.Entitys;
using RuoVea.OmiApi.Article.Service;
using RuoVea.OmiApi.Article.Service.Dto;
using RuoVea.OmiArticle.Attributes;
using RuoVea.OmiArticle.Language;

namespace RuoVea.OmiArticle.Controllers;

/// <summary>
/// 分类管理
/// </summary>
[AppRoute]
public class CategoryController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly CategoryService _thisService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisService"></param>
    public CategoryController(CategoryService thisService)
    {
        _thisService = thisService;
    }

    /// <summary>
    /// 页面
    /// </summary>
    [HttpGet(nameof(Index))]
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<CategoryDto>>> GetPagesAsync()
    {
        RestfulResult<PageResult<CategoryDto>> restfulResult = new() { Code = CodeStatus.OK };
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.no_upload_data);

        CategoryParam data = JsonConvert.DeserializeObject<CategoryParam>(dataStr) ?? new CategoryParam { PageNo = 1, PageSize = 10 };

        restfulResult.Data = await _thisService.GetPagesAsync(data);
        return restfulResult;
    }

    /// <summary>
    /// 获取列表🔹
    /// </summary>
    /// <returns></returns>
    [HttpGet("list")]
    public async Task<RestfulResult<List<CategoryOutput>>> GetList([FromQuery] CategoryQuery data)
    {
        RestfulResult<List<CategoryOutput>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _thisService.GetList(data);
        return restfulResult;
    }
    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(CategoryInputDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _thisService.AddDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync(CategoryInputDto data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _thisService.UpdateDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(EntityBaseId data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        if (data == null) return new RestfulResult { Code = CodeStatus.BadRequest, Message = i18n.no_data_obtained };
        try
        {
            await _thisService.DeleteDataAsync(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Message = ex.Message;
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            return restfulResult;
        }
    }
}
